<template>
    <div v-if="isAuth(['ROOT',  'VIDEO_DIAGNOSE:SELECT'])">
		<el-form :inline="true" :model="dataForm" :rules="dataRule" ref="dataForm">
		    <el-form-item prop="name">
		        <el-input
		            v-model="dataForm.drugname"
		            placeholder="药品名称"
		            size="medium"
		            class="input"
		            clearable="clearable"
		        />
		    </el-form-item>
			<el-form-item prop="drugid">
			    <el-input
			        v-model="dataForm.drugid"
			        placeholder="药品编号"
			        size="medium"
			        class="input"
			        clearable="clearable"
			    />
			</el-form-item>
			<el-form-item>
			    <el-button type="primary" @click="searchHandle()">查询</el-button>
				<el-button
				    size="medium"
				    type="primary"
				    :disabled="!isAuth(['ROOT'])"
				    @click="addHandle()"
				>
				    新增
				</el-button>
			</el-form-item>
		</el-form>
        <el-table
            :data="dataList"
            border
            v-loading="dataListLoading"
            @selection-change="selectionChangeHandle"
            :cell-style="{ padding: '3px 0' }"
            style="width: 100%;"
        >
            <el-table-column
                type="selection"
                :selectable="selectable"
                header-align="center"
                align="center"
                width="50"
            />
            <el-table-column type="index" header-align="center" align="center" width="100" label="序号">
                <template #default="scope">
                    <span>{{ (pageIndex - 1) * pageSize + scope.$index + 1 }}</span>
                </template>
            </el-table-column>
			<el-table-column header-align="center" align="center" label="药品编号" min-width="180">
			    <template #default="scope">
			        <span>{{ scope.row.drugid }}</span>
			    </template>
			</el-table-column>
			<el-table-column header-align="center" align="center" label="药品名称" min-width="120">
			    <template #default="scope">
			        <span>{{ scope.row.drugname }}</span>
			    </template>
			</el-table-column>
			<el-table-column header-align="center" align="center" label="药品规格" min-width="120">
			    <template #default="scope">
			        <span>{{ scope.row.drugspec }}</span>
			    </template>
			</el-table-column>
			<el-table-column header-align="center" align="center" label="生产厂家" min-width="120">
			    <template #default="scope">
			        <span>{{ scope.row.drugmade }}</span>
			    </template>
			</el-table-column>
			<el-table-column prop="drugstatus" header-align="center" align="center" label="药品类别" min-width="120" />
			<el-table-column
			    prop="remark"
			    header-align="center"
			    align="left"
			    label="药品介绍"
			    min-width="430"
			    :show-overflow-tooltip="true"
			/>
			<el-table-column header-align="center" align="center" width="150" label="药品操作">
			    <template #default="scope">
			        <el-button
			            type="text"
			            :disabled="!isAuth(['ROOT', 'MEDICAL_DEPT:UPDATE'])"
			            @click="updateHandle(scope.row.id)"
			        >
			            修改
			        </el-button>
			        <el-button
			            type="text"
			            :disabled="!isAuth(['ROOT', 'MEDICAL_DEPT:DELETE']) || scope.row.emps > 0"
			            @click="deleteHandle(scope.row.id)"
			        >
			            删除
			        </el-button>
			    </template>
			</el-table-column>
        </el-table>
        <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :current-page="pageIndex"
            :page-sizes="[10, 20, 50]"
            :page-size="pageSize"
            :total="totalCount"
            layout="total, sizes, prev, pager, next, jumper"
        ></el-pagination>

        <add-or-update ref="addOrUpdate" @refreshDataList="loadDataList"></add-or-update>
    </div>
</template>

<script>
import AddOrUpdate from './drug-add-or-update.vue';
export default {
    components: {
        AddOrUpdate
    },
    data: function() {
        return {
            dataForm: {
                drugname: '',
                drugid: ''
            },
            dataList: [],
            pageIndex: 1,
            pageSize: 10,
            totalCount: 0,
            dataListLoading: false,
            dataListSelections: [],
            dataRule: {
                name: [{ required: false, pattern: '^[\u4e00-\u9fa5]{1,10}$', message: '药品名称格式错误' }],
				drugid: [{ required: false, pattern: '^[0-9]{1,13}$', message: '药品编号格式错误' }]
            }
        };
    },
    methods: {
        loadDataList: function() {
            let that = this;
            that.dataListLoading = true;
            let data = {
                page: that.pageIndex,
                length: that.pageSize,
				drugname: that.dataForm.drugname == '' ? null : that.dataForm.drugname,
				drugid: that.dataForm.drugid == '' ? null : that.dataForm.drugid
            };
        
            that.$http('/drug/searchByPage', 'POST', data, true, function(resp) {
                let result = resp.result;
				let temp = {
					'1': '处方',
					'2': '非处方'
				}
                for (let one of result.list) {
                    one.drugstatus = temp[one.drugstatus+''];
                }
                that.dataList = result.list;
                that.totalCount = result.totalCount;
                that.dataListLoading = false;
            });
        },
		sizeChangeHandle: function(val) {
		    this.pageSize = val;
		    this.pageIndex = 1;
		    this.loadDataList();
		},
		currentChangeHandle: function(val) {
		    this.pageIndex = val;
		    this.loadDataList();
		},
		addHandle: function() {
		    //使用$nextTick函数可以保证init函数中的代码都能执行完，不会强制结束
		    this.$nextTick(() => {
		        this.$refs.addOrUpdate.init();
		    });
		},
		updateHandle: function(id) {
		    this.$nextTick(() => {
		        this.$refs.addOrUpdate.init(id);
		    });
		},
		searchHandle: function() {
		    this.$refs['dataForm'].validate(valid => {
		        if (valid) {
		            this.$refs['dataForm'].clearValidate();
		            if (this.dataForm.drugname == '') {
		                this.dataForm.drugname = null;
		            }
					if (this.dataForm.drugid == '') {
					    this.dataForm.drugid = null;
					}
		            if (this.pageIndex != 1) {
		                this.pageIndex = 1;
		            }
		            this.loadDataList();
		        } else {
		            return false;
		        }
		    });
		},
		selectionChangeHandle: function(val) {
		    this.dataListSelections = val;
		},
		selectable: function(row, index) {
		    if (row.emps > 0) {
		        return false;
		    }
		    return true;
		},
		deleteHandle: function(id) {
		    let that = this;
		    let ids = id
		        ? [id]
		        : that.dataListSelections.map(item => {
		              return item.id;
		          });
		    if (ids.length == 0) {
		        ElMessage({
		            message: '没有选中记录',
		            type: 'warning',
		            duration: 1200
		        });
		    } else {
		        ElMessageBox.confirm('确定要删除选中的记录？', '提示信息', {
		            confirmButtonText: '确定',
		            cancelButtonText: '取消',
		            type: 'warning'
		        }).then(() => {
		            that.$http('/drug/deleteByIds', 'POST', { ids: ids }, true, function(resp) {
		                ElMessage({
		                    message: '操作成功',
		                    type: 'success',
		                    duration: 1200,
		                    onClose: () => {
		                        that.loadDataList();
		                    }
		                });
		            });
		        });
		    }
		},

    },
    created: function() {
        this.loadDataList();
    }
};
</script>

<style></style>
